package controller;

import java.sql.ResultSet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import core.*;

public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static PgSQL db = PgSQL.getInstance();

	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		CurrentUser user = new CurrentUser(request);
		if(user.isLoggedIn()){
			View.showMessage(request, response, "You have already got an account.", "Index");
			return;
		}
		
		String username = "";
		String email = "";
		String password = "";
		String confirm_password = "";
		String Query = "";
		ResultSet rs;
		
		try {
			username = request.getParameter("username").toString();
			email = request.getParameter("email").toString();
			password = request.getParameter("password").toString();
			confirm_password = request.getParameter("confirm_password").toString();
			
			if(username.length() < Config.usernameMinLength || username.length() > Config.usernameMaxLength){
				View.showMessage(request, response, "Your user name is either too long or too short. Acceptable length is between " + Config.usernameMinLength + " and " + Config.usernameMaxLength + ".", "back");
				return;
			}else if(email.length() > Config.emailMaxLength){
				View.showMessage(request, response, "Your email address is too long. Acceptable length is under " + Config.emailMaxLength + ".", "back");
				return;
			}else if(password.length() < Config.passwordMinLength || password.length() > Config.passwordMaxLength){
				View.showMessage(request, response, "Your password is either too long or too short. Acceptable length is between " + Config.passwordMinLength + " and " + Config.passwordMaxLength + ".", "back");
				return;
			}

			int pos_at = email.indexOf('@');
			int pos_dot = email.indexOf('.', pos_at);
			if(pos_at < 0 || pos_dot < 0){
				View.showMessage(request, response, "Invalid email address.", "back");
			}
			
			if (!password.equals(confirm_password)) {
				View.showMessage(request, response, "Your passwords mismatch each other. Please confirm it.", "back");
				return;
			}

			Query = "SELECT * FROM qz_user WHERE email=?";
			rs = db.query(Query, email);
			if (rs.next()) {
				View.showMessage(request, response, "Sorry, your email address has been registered.", "back");
				return;

			} else {
				if (user.register(username, email, password)){
					response.sendRedirect("Index");
					return;
				}

				View.showMessage(request, response, "We are sorry to inform you that your registration failed due to our server problems. Please try again later.", "Index");				
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		CurrentUser user = new CurrentUser(request);
		if(user.isLoggedIn()){
			View.showMessage(request, response, "You have already got an account.", "Index");
			return;
		}
		
		View.show(request, response, "register");
	}
}
